﻿using mozhi.smarterp.Finance.Treasuries.Services;
using mozhi.smarterp.Finance.Treasuries.Repositories;
using System;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using System.Collections.Generic;

namespace mozhi.smarterp.Finance.Treasuries.Collections
{
    public class CollectionAppService : FinanceAppService, ICollectionAppService
    {
        private ICollectionRepository _repository;
        private CollectionManager _manager;

        public CollectionAppService(ICollectionRepository repository , CollectionManager manager)
        {
            _repository = repository;
            _manager = manager;
        }

        public async Task<CollectionDto> CreateAsync(CollectionCreateDto input)
        {
            var collection = await _manager.GetAsync(null, input.Date, input.BankAccountId, input.TradingPartnerType, input.SupplierId,
                input.EmployeeId, input.CustomerId, input.CurrencyId, input.ExchangeRate, input.IsAdvanced, input.IsRefund, input.Comment);

            input.Items = input.Items ?? new List<CollectionItemCreateDto>();

            var cllectionItems = new List<CollectionItem>();

            foreach (var item in input.Items)
            {
                var collectionItem = await _manager.GetItemAsync(null, item.AmountFr, item.Remark);

                cllectionItems.Add(collectionItem);
            }

            collection.SetItems(cllectionItems);

            collection = await _repository.InsertAsync(collection);

            var result = ObjectMapper.Map<Collection, CollectionDto>(collection);

            return result;
        }

        public Task DeleteAsync(Guid id)
        {
            throw new NotImplementedException();
        }

        public async Task<CollectionDto> GetAsync(Guid id)
        {
            var collection = await _repository.GetAsync(id);

            var result = ObjectMapper.Map<Collection, CollectionDto>(collection);

            return result;
        }

        public async Task<PagedResultDto<CollectionDto>> GetListAsync(CollectionPagedResultRequestDto filter)
        {
            var collections = await _repository.GetPagedListAsync(filter.Keyword, filter.Number, filter.CurrencyId, filter.SkipCount, filter.MaxResultCount, filter.Sorting);

            var count = await _repository.GetCountAsync(filter.Keyword, filter.Number, filter.CurrencyId, null);

            var items = ObjectMapper.Map<List<Collection>, List<CollectionDto>>(collections);

            return new PagedResultDto<CollectionDto>()
            {
                Items = items,
                TotalCount = count
            };
        }

        public async Task<CollectionDto> UpdateAsync(CollectionUpdateDto input)
        {
            var collection = await _manager.GetAsync(input.Id, input.Date, input.BankAccountId, input.TradingPartnerType, input.SupplierId,
                input.EmployeeId, input.CustomerId, input.CurrencyId, input.ExchangeRate, input.IsAdvanced, input.IsRefund, input.Comment);

            input.Items = input.Items ?? new List<CollectionItemUpdateDto>();

            var cllectionItems = new List<CollectionItem>();

            foreach (var item in input.Items)
            {
                var collectionItem = await _manager.GetItemAsync(input.Id, item.AmountFr, item.Remark);

                cllectionItems.Add(collectionItem);
            }

            collection.SetItems(cllectionItems);

            collection = await _repository.UpdateAsync(collection);

            var result = ObjectMapper.Map<Collection, CollectionDto>(collection);

            return result;
        }
    }
}
